package com.atguigu.exam.service.impl;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Notice;
import com.atguigu.exam.mapper.NoticeMapper;
import com.atguigu.exam.service.NoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.logging.LogFileWebEndpoint;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公告服务实现类
 */
@Service
@Slf4j
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    /**
     * 获取最新的公告
     *
     * @param limit
     * @return
     */
    @Override
    public List<Notice> getLatestNotice(Integer limit) {
        //拼接条件
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //激活状态的公告
        lambdaQueryWrapper.eq(Notice::getIsActive, 1);
        //最新时间顺序排序
        lambdaQueryWrapper.orderByDesc(Notice::getUpdateTime);
        lambdaQueryWrapper.orderByDesc(Notice::getCreateTime);
        //限制查询几条
        lambdaQueryWrapper.last("limit " + limit);
        //查询
        List<Notice> list = list(lambdaQueryWrapper);
        //日志输出
        log.info("查询最新公告成功，查询结果为：{}", list);
        //返回结果
        return list;
    }


    /**
     * 获取启用公告
     *
     * @return
     */
    @Override
    public List<Notice> getActiveNotice() {
        //拼接条件
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //激活状态的公告
        lambdaQueryWrapper.eq(Notice::getIsActive, 1);
        //最新时间顺序排序
        lambdaQueryWrapper.orderByDesc(Notice::getUpdateTime);
        lambdaQueryWrapper.orderByDesc(Notice::getCreateTime);
        //查询
        List<Notice> list = list(lambdaQueryWrapper);
        //日志输出
        log.info("获取启用公告成功，查询结果为：{}", list);
        //返回结果
        return list;
    }

    /**
     * 根据id获取公告
     *
     * @param id
     * @return
     */
    @Override
    public Notice getNoticeById(Integer id) {
        //获取公告
        Notice notice = getById(id);
        //判断是否获取成功
        if (notice == null) {
            throw new RuntimeException("该公告不存在!");
        }
        //返回结果
        return notice;
    }

    /**
     * 发布新公告
     *
     * @param notice
     */
    @Override
    public void addNotice(Notice notice) {
        //设置默认启用状态
        if (notice.getIsActive() == null) {
            notice.setIsActive(true);
        }
        //设置默认公告优先级
        if (notice.getPriority() == null) {
            notice.setPriority(0);
        }
        //设置默认公告类型
        if (notice.getType() == null || notice.getType().isEmpty()) {
            notice.setType("NOTICE");
        }

        boolean saved = save(notice);
        //判断是否保存成功
        if (!saved) {
            throw new RuntimeException("添加公告失败");
        }
        log.info("添加公告成功");
    }

    /**
     * 更新公告信息
     *
     * @param notice
     */
    @Override
    public void updateNotice(Notice notice) {

        //设置默认启用状态
        if (notice.getIsActive() == null) {
            notice.setIsActive(true);
        }
        //设置默认公告优先级
        if (notice.getPriority() == null) {
            notice.setPriority(0);
        }
        //设置默认公告类型
        if (notice.getType() == null || notice.getType().isEmpty()) {
            notice.setType("NOTICE");
        }

        boolean updated = updateById(notice);
        //判断更新是否成功
        if (!updated) {
            throw new RuntimeException("更新公告失败");
        }
        log.info("更新公告成功，更新后的公告为：{}", notice);
    }

    /**
     * 切换公告状态
     *
     * @param id
     * @param isActive
     */
    @Override
    public void updateNoticeStatus(Integer id, Boolean isActive) {
        //拼接条件
        LambdaUpdateWrapper<Notice> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Notice::getId, id);
        lambdaUpdateWrapper.set(Notice::getIsActive, isActive);
        //切换状态
        boolean updated = update(lambdaUpdateWrapper);
        //判断是否更新成功
        if (!updated) {
            throw new RuntimeException("切换状态失败");
        }
        //日志输出
        log.info("切换状态成功,切换的公告id为:{},切换后的状态为:{}", id, isActive);

    }

    /**
     * 获取所有公告
     *
     * @return
     */
    @Override
    public List<Notice> getAllNotices() {
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //最新时间顺序排序
        lambdaQueryWrapper.orderByDesc(Notice::getUpdateTime);
        lambdaQueryWrapper.orderByDesc(Notice::getCreateTime);
        //获取结果
        List<Notice> list = list(lambdaQueryWrapper);
        //输出日志
        log.info("获取所有公告成功,获取的结果为:{}", list);
        //返回结果
        return list;
    }

    /**
     * 删除公告
     *
     * @param id
     */
    @Override
    public void deleteNotice(Long id) {
        boolean deleted = removeById(id);
        if (!deleted) {
            throw new RuntimeException("删除公告失败");
        }
        log.info("删除公告成功,删除的id为:{}", id);
    }
}